package com.llkj.qingnianguoxue.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.HotRankingAdapter;
import com.llkj.qingnianguoxue.base.BaseFragment;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.HotRankingBean;
import com.llkj.qingnianguoxue.bean.ResponseBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.login.LoginActivity;
import com.llkj.qingnianguoxue.util.ClickViewCallback;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 热门视频列表
 */
public class HotVideoFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener, ClickViewCallback {
    private static final int REQUEST_LOGIN_CODE = 7892;
    private static final int REQUEST_VIDEO_CODE = 7891;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvActions;
    private HotRankingAdapter rankingAdapter;
    private List<HotRankingBean.RankingBean> mRankData;
    private int totalSize, mPage;
    private int mCurrentPosition,mShopNum;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_hot_video, container, false);

        initView();
        initListener();

        onRefresh();
        return rootView;
    }

    private void initView() {
        refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvActions = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        int padding = DisplayUtil.dip2px(getActivity(), 10);
        lvActions.setPadding(padding, 0, padding, padding);
        lvActions.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

        mRankData = new ArrayList<>();
        rankingAdapter = new HotRankingAdapter(R.layout.item_hot_ranking, mRankData, this);
        rankingAdapter.setType(1);
        rankingAdapter.setTagVisible(1);
        lvActions.setAdapter(rankingAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        rankingAdapter.setOnLoadMoreListener(this);
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getVideoData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getVideoData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, rankingAdapter, null, lvActions, totalSize);
    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();
        startActivityForResult(LoginActivity.class, new Bundle(),REQUEST_LOGIN_CODE);
    }

    @Override
    public void clickViewCallback(int position, int type, View view) {
        boolean isLogin = MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN);
        if (!isLogin) {
            dialogConfirm(null, null, null, null).show();
            return;
        }
        HotRankingBean.RankingBean item = mRankData.get(position);
        mCurrentPosition = position;
        switch (type) {
            case 1:
                if (1 == item.getType()) {
                    addCart(item.getId());
                } else if (2 == item.getType()) {
                    delCart(item.getShopping_id());
                }
                break;
            case 2:
                Bundle bundle = new Bundle();
                bundle.putString("id", item.getId());
                startActivityForResult(VideoDetailActivity.class, bundle, REQUEST_VIDEO_CODE);
                break;
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == REQUEST_VIDEO_CODE) {
                onRefresh();
            }
        }else if(resultCode == getActivity().RESULT_OK && requestCode == REQUEST_LOGIN_CODE){
            onRefresh();
        }
    }

    private void getVideoData() {
        HttpMethod.hotVideo(getActivity(), this, String.valueOf(mPage));
    }

    /**
     * 加入购物车
     */
    private void addCart(String videoId) {
        HttpMethod.addShopping(getActivity(), this, "1", videoId);
    }

    /**
     * 删除购物车
     */
    private void delCart(String cartId) {
        HttpMethod.delShopping(getActivity(), this, cartId,"2");
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_INDEX_HOT_VIDEO) {
            HotRankingBean hotRankingBean = GsonUtil.GsonToBean(data, HotRankingBean.class);
            if (hotRankingBean != null) {
                String shopNum = hotRankingBean.getShopping_num();
                ((HotRankingActivity) getActivity()).setShopCount(shopNum);
                mShopNum = Integer.parseInt(shopNum);

                totalSize = Integer.parseInt(hotRankingBean.getNum());
                if (mPage == 1) {
                    mRankData.clear();
                    LoadDataHelper.refreshComplete(rankingAdapter, mRankData, refreshLayout, lvActions);
                } else {
                    rankingAdapter.setEnableLoadMore(true);
                    rankingAdapter.loadMoreComplete();
                }
                mRankData.addAll(hotRankingBean.getVideo());
                rankingAdapter.notifyDataSetChanged();
            }
        } else if (method == HttpMethod.HTTP_ORDER_ADD_SHOPPING) {//9.2视频/图书加购物车
            ResponseBean responseBean = GsonUtil.GsonToBean(data, ResponseBean.class);
            if (responseBean != null) {
                ToastUtil.makeLongText(getActivity(), "成功加入购物车");
                HotRankingBean.RankingBean item = mRankData.get(mCurrentPosition);
                item.setShopping_id(responseBean.getShopping_id());
                item.setType(2);
                rankingAdapter.notifyItemChanged(mCurrentPosition);

                mShopNum++;
                ((HotRankingActivity) getActivity()).setShopCount(String.valueOf(mShopNum));
            }
        } else if (method == HttpMethod.HTTP_USER_DEL_SHOPPING) {//8.13删除购物车商品
            ToastUtil.makeLongText(getActivity(), "成功移除购物车");
            HotRankingBean.RankingBean item = mRankData.get(mCurrentPosition);
            item.setType(1);
            rankingAdapter.notifyItemChanged(mCurrentPosition);

            mShopNum--;
            ((HotRankingActivity) getActivity()).setShopCount(String.valueOf(mShopNum));
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        super.onhttpFailed(message, method);
        if (!StringUtils.isEmpty(message)) {
            ToastUtil.makeLongText(getActivity(), message);
        }
    }
}
